﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Engine.Common;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Engine.Core.Result;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Engine.Service;
using Sharp.Domain.PlatformBoundedContext.WorkFlowModule.Engine.Xpdl;
using Sharp.Infrastructure.IoC;

namespace Sharp.Application.Platform
{
    /// <summary>
    /// 引擎和应用端的交互服务类
    /// 示例代码，请勿直接作为生产项目代码使用。
    /// </summary>
    public class WfAppInteropService
    {

        /// <summary>
        /// 工作流运行
        /// </summary>
        /// <param name="runner"></param>
        /// <returns></returns>
        public WfExecutedResult RunProcess(WfAppRunner runner, IDictionary<string, string> conditions = null)
        {
            var result = new WfExecutedResult();
            var wfService = ServiceLocator.Instance.GetService<IWorkflowService>();
            var nodeViewList = wfService.GetNextActivityTree(runner, conditions).ToList<NodeView>();

            foreach (var node in nodeViewList)
            {
                var performerList = wfService.GetPerformerList(node);       //根据节点角色定义，读取执行者列表
                Dictionary<string, PerformerList> dict = new Dictionary<string, PerformerList>();
                dict.Add(node.ActivityGUID, performerList);
                runner.NextActivityPerformers = dict;

                if (node.IsSkipTo)
                {
                    result = wfService.JumpProcess(runner);
                }
                else
                {
                    result = wfService.RunProcessApp(runner);
                }
            }
            return result;
        }

    }
}
